package com.ibm.ems.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ibm.ems.dto.EventBean;
import com.ibm.ems.dto.Feedback;

public class FeedbackDAO {
	
	private Connection con;
	

	public FeedbackDAO(Connection conn) {
		this.con = conn;
	}
	public Feedback getFeedbackDetails(Feedback e) throws SQLException{
		PreparedStatement pstmt = null;
		
		ResultSet rs = null;
		try {
			pstmt = con.prepareStatement("select comments from feedback where event_id=?");
			//pstmt.setInt(1, e.getEventId());
		
			rs = pstmt.executeQuery();
		
			while (rs.next()) {
				e.setComments(rs.getString("comments"));
				
				
			}
		} catch (SQLException ex) {
			throw ex;
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
			} catch (Exception ex1){
				
			}
		}
		
		con.close();
//		Speaker speaker = new Speaker();
//		speaker.setEventId(0);
//		return speaker;
		return e;
	}
	
	public List<EventBean> getListOfEventsForFeedback(){
		List<EventBean> list = new ArrayList<EventBean>();
		EventBean event1 = new EventBean();
		
		try {
			PreparedStatement pstmt = con.prepareStatement("select EVENT_ID, EVENT_NAME, EVENT_DESCRIPTION from EVENTS where (sysdate-end_date)<10 and EVENT_ID in(select EVENT_ID from ENROLLMENT where USER_NAME = ? and EVENT_ID NOT IN ( select EVENT_ID from feedback where NAME = ?))");
			pstmt.setString(1, "U1002");
			pstmt.setString(2, "U1002");
			
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				event1 = new EventBean();
				event1.setEventId(rs.getString("EVENT_ID"));
				event1.setEventName(rs.getString("EVENT_NAME"));
				event1.setEventDesc(rs.getString("EVENT_DESCRIPTION"));
				list.add(event1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
public void submitFeedback(String eventID, String feedback) {
		
		try {
			PreparedStatement pstmt = con.prepareStatement("insert into feedback values(?,?,?)");
			pstmt.setString(1, "U1002");
			pstmt.setString(2, feedback);
			pstmt.setString(3, eventID);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
